package cn.itcast.controller;

import cn.itcast.pojo.Clazz;
import cn.itcast.pojo.ClazzQueryParam;
import cn.itcast.pojo.PageResult;
import cn.itcast.pojo.Result;
import cn.itcast.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;


    /*添加班级*/
    @PostMapping

    public Result save(@RequestBody Clazz clazz) {
        log.info("添加班级：{}", clazz.getName()); // 记录操作日志
        clazzService.save(clazz);
        return Result.success();
    }

    /*查询所有班级*/
    @GetMapping("/list")
    public Result findAll() {
        List<Clazz> clazzList = clazzService.findAll();
        return Result.success(clazzList);
    }

    /*分页查询*/
    @GetMapping
    public Result page(ClazzQueryParam clazzQueryParam) {
        PageResult<Clazz> pageResult = clazzService.page(clazzQueryParam);
        return Result.success(pageResult);
    }

    /*根据id查询*/
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        Clazz clazz = clazzService.findById(id);
        return Result.success(clazz);
    }

    //    修改班级
    @PutMapping
    public Result updateClazz(@RequestBody Clazz clazz) {
        clazzService.updateClazz(clazz);
        return Result.success();

    }

    /*删除班级*/
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("删除班级id={}",id);
        clazzService.deleteById(id);
        return Result.success();
    }
}
